package com.ljx.vod.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.model.vod.Course;
import com.ljx.model.vod.CourseDescription;
import com.ljx.model.vod.Subject;
import com.ljx.model.vod.Teacher;
import com.ljx.vo.vod.CourseFormVo;
import com.ljx.vo.vod.CoursePublishVo;
import com.ljx.vo.vod.CourseQueryVo;
import com.ljx.vod.mapper.CourseMapper;
import com.ljx.vod.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ljx
 * @since 2023-04-06
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private SubjectService subjectService;;

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;


    /**
     * 分页查询
     * @param page
     * @param limit
     * @param courseQueryVo
     * @return
     */
    @Override
    public Map<String, Object> findPage(Long page, Long limit, CourseQueryVo courseQueryVo) {
        Page<Course> pageParam = new Page<>(page,limit);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        Long subjectId = courseQueryVo.getSubjectId();
        Long subjectParentId = courseQueryVo.getSubjectParentId();
        String title = courseQueryVo.getTitle();
        Long teacherId = courseQueryVo.getTeacherId();
        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        queryWrapper.orderByDesc("create_time");
        Page<Course> pages = baseMapper.selectPage(pageParam, queryWrapper);
        long totalCount = pages.getTotal();//总记录数
        long totalPage = pages.getPages();//总页数
        //每页数据集合
        List<Course> records = pages.getRecords();
        List<Course> courses = records.stream().map(course -> {
            Subject subjectOne = subjectService.getById(course.getSubjectId());
            if (subjectOne != null) {
                course.getParam().put("subjectTitle", subjectOne.getTitle());
            }
            Subject serviceTwo = subjectService.getById(course.getSubjectParentId());
            if (serviceTwo != null) {
                course.getParam().put("subjectParentTitle", serviceTwo.getTitle());
            }
            Teacher teacherServiceById = teacherService.getById(course.getTeacherId());
            if (teacherServiceById != null) {
                course.getParam().put("teacherName", teacherServiceById.getName());
            }
            return course;
        }).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("records",courses);
        return map;
    }

    /**
     * 添加课程
     * @param courseFormVo
     * @return
     */
    @Override
    public Long saveCourseInfo(CourseFormVo courseFormVo) {
        //保存课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo, course);
        baseMapper.insert(course);

        //保存课程详情信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setCourseId(course.getId());
        courseDescriptionService.save(courseDescription);

        //返回课程id
        return course.getId();
    }

    /**
     *根据课程id获取可视类的课程
     * @param id
     * @return
     */
    @Override
    public CourseFormVo getCourseById(Long id) {
        Course course = baseMapper.selectById(id);
        if(course == null) return null;
        CourseFormVo courseFormVo = new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        //以下是判断是否有描述
        CourseDescription descriptionService = courseDescriptionService.getById(id);
        if(descriptionService != null){
            courseFormVo.setDescription(descriptionService.getDescription());
        }
        return courseFormVo;
    }

    /**
     * 修改课程
     * @param courseFormVo
     */
    @Override
    public Long updateCourseById(CourseFormVo courseFormVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);
        //修改课程表描述表
        QueryWrapper<CourseDescription> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",course.getId());
        CourseDescription description = courseDescriptionService.getOne(queryWrapper);
        description.setDescription(courseFormVo.getDescription());
        courseDescriptionService.updateById(description);
        return course.getId();
    }

    /**
     * 获取发布课程的信息
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    /**
     * 发布课程，相当于修改状态码
     * @param id
     * @return
     */
    @Override
    public boolean publishCourseById(Long id) {
        Course course = new Course();
        course.setId(id);
        course.setPublishTime(new Date());
        course.setStatus(1);
        return this.updateById(course);
    }

    @Override
    public void removeCourseById(Long id) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(id);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(id);
        //根据课程id删除描述
        courseDescriptionService.removeById(id);
        //根据课程id删除课程
        baseMapper.deleteById(id);
    }
}
